/*if (!isWechat() || !isAlipay())
{
	GotoError('E00');
}*/
/*	
if (isInvalid(getCookie('deviceNO')))
{
	GotoError('E01');
}*/

String.prototype.format = function(args) 
{
    var result = this;
    if (arguments.length > 0) {    
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if(args[key]!=undefined){
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({[" + i + "]})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    
    return result;
}

String.prototype.trim = function() 
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

window.addEventListener('orientationchange', function(event){
    if ( window.orientation == 180 || window.orientation==0 ) {
        //alert("竖屏");
    	//initTouchListener();
    	//sliderInit();
    }
    if( window.orientation == 90 || window.orientation == -90 ) {
        //alert("横屏");
    	//initTouchListener();
    	//sliderInit();
    }
    //initTouchListener();
    //sliderInit();
});

function toTop()
{
	javascript:scroll(0, 0);
}

// _obj 用户存放请求结束后需要用到的其他数据
/*function GetRequestData(params, _callback, _obj)
{
	var _data;
	
	params.dNo = getCookie('deviceNO');
	
	if (isInvalid(params.dNo))
	{
		GotoError('E01');
		
		return;
	}
	
	if (params.rCode != WMALL000017 && params.rCode != WMALL000018)
	{
		params.memberNo = getCookie('memberNO');
		
		if (isInvalid(params.memberNo))
		{
			GotoError('E02');
		
			return;
		}
	}
	
	var date = new Date();
	params.timestamp = date.getTime();
	
	params.ver = 1;
	
	var _param = {};
	_param.reqMsg = JSON.stringify(params);
    alert(JSON.stringify(_param));
	$.ajax({
		url: URL_GET_API,
		type: "POST",
		data: _param,
		dataType:"jsonp",
		jsonpCallback:"vempn",
		success: function(data)
		{		alert(JSON.stringify(data));
			if (data != null)
			{
				if(data[JSON_KEY_CODE] == 'C1000') 
				{
					ShowTip(data.msg);
				} 
				else if (data[JSON_KEY_CODE] == 'C0009')
				{
					GotoUrl('E05');
				} 
				else if (data[JSON_KEY_CODE] == 'C0020')
				{
					GotoUrl('E04');
				}
				else 
				{
					_callback(data, _obj);
				}
			}
		},
		failure: function()
		{
			_data = {};
			
			_data[JSON_KEY_CODE] = RESPOND_CODE_AJAX_FAILURE;
			
			_callback(_data, _obj);
		}
	});
	
}

function isWechat()
{
	var ua = navigator.userAgent.toLowerCase();
	if(ua.match(/MicroMessenger/i)=="micromessenger") {
		return true;
 	} else {
		return false;
	}
}

function isAlipay()
{
	var ua = navigator.userAgent.toLowerCase();
	if(ua.match(/Alipay/i)=="alipay") {
		return true;
 	} else {
		return false;
	}
}*/

function GetUrlParams(url)
{
	var sParams = url.split('?')[1];
	
	if (typeof(sParams) == 'undefined')
	{
		return null;
	}
	
	var paramsObj = {};
	
	var params = sParams.split('&');
	
	for (var i=0; i<params.length; i++)
	{
		var temp = params[i].split('=');
		
		paramsObj[temp[0]] = temp[1];
	}
	
	return paramsObj;
}

function GotoUrl(url)
{
	window.location.href = url;
}

function GotoPay(orderInfo)
{	
	console.log(orderInfo);
	var url = "../mobile/GotoPay?OrderInfo="+orderInfo;
	//window.location.href = "${ctx }/mobile/GotoPay?OrderInfo=" + orderInfo;
	GotoUrl(url);
}
/*function GotoCategory(goodsType)
{
	var url = "category.jsp?GoodsType=" + goodsType;
	
	GotoUrl(url);
}

function GotoPay(orderInfo)
{	
	var url = "pay.jsp?OrderInfo=" + orderInfo;
	
	GotoUrl(url);
}

function GotoResult(payInfo)
{	
	var url = "result.jsp?PayInfo=" + payInfo;
	
	GotoUrl(url);
}

function GotoError(error)
{
	var url = "error.jsp?error=" + error;
	GotoUrl(url);
}*/

function closePage(){
	/*document.querySelector('#closeWindow').onclick = function () {
	    wx.closeWindow();
	};*/
	WeixinJSBridge.call('closeWindow');
}

function isInvalid(v)
{	
	if (typeof(v) == "undefined")
	{
		return true;
	}
	
	if (!v && v != 0)
	{
		return true;
	}

	return false;
}

/*var base64 = new Base64(); */

function GetCartCount()
{
	var cartInfo = GetCartInfo();
	
	var totalCount = 0;
	
	if (cartInfo != null)
	{
		for (var i=0; i<cartInfo.length; i++)
		{
			totalCount += parseInt(cartInfo[i].Count);
		}
	}
	
	return totalCount;
}

function GetCartInfo()
{
	var cartInfo = getCookie('CartInfo');	
	
	if (isInvalid(cartInfo))
	{
		return null;
	}
	else
	{
		//return JSON.parse(base64.decode(decodeURIComponent(cartInfo)));
		return JSON.parse(decodeURIComponent(cartInfo));
	}
}

function AddToCart(id, num, maxNum)
{
	var isExist = false;
	
	var cartInfo = GetCartInfo();
	
	if (cartInfo == null)
	{
		cartInfo = new Array();
	}
	else
	{
		for (var i=0; i<cartInfo.length; i++)
		{
			if (cartInfo[i].commodityId == id)
			{
				isExist = true;
				
				var newNum = cartInfo[i].Count + num;
				
				if (newNum > maxNum)
				{
					newNum = maxNum;
				}
				
				cartInfo[i].Count = newNum;
				
				break;
			}
		}
	}
	
	if (!isExist)
	{
		var cartItem = {};
		
		cartItem.commodityId = id;
		cartItem.Count = num;
		
		cartInfo.push(cartItem);
	}  
	
	var s = JSON.stringify(cartInfo);
	
	console.log(s);
	//setCookieDefault("CartInfo", base64.encode(s));
	setCookieDefault("CartInfo", s);
}

function RemoveCart(id)
{
	var cartInfo = GetCartInfo();
	
	if (cartInfo == null)
	{
		return;
	}
	else
	{
		for (var i=0; i<cartInfo.length; i++)
		{
			if (cartInfo[i].commodityId == id)
			{
				cartInfo.splice(i, 1);
				
				break;
			}
		}
	}
	
	var s = JSON.stringify(cartInfo);
	
	//setCookieDefault("CartInfo", base64.encode(s));
	setCookieDefault("CartInfo", s);
}

function ShowTip(msg, bottom)
{
	if (isInvalid(bottom))
	{
		bottom = 60;
	}
	
	var tip = $('<div></div>').addClass('tip').html(msg);
	
	tip.css('bottom', bottom + 'px');
	
	$("body").append(tip);
	
	setTimeout(function(){
		tip.remove();
		}, 3000);
}

function Show(msg, bottom)
{
	if (isInvalid(bottom))
	{
		bottom = '50%';
	}
	
	var tip = $('<div></div>').addClass('tip').html(msg);
	
	tip.css('bottom', bottom );
	
	$("body").append(tip);
	
	setTimeout(function(){
		tip.remove();
		}, 5000);
}